﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Microsoft.SharePoint;
using System.Web;

namespace SPThumbnailExtender.Interne
{
    internal abstract class ThumbnailBase
    {

        #region private fields
        private Uri _siteUri;
        private Uri _fileUri; 
        private int _iWidth;
        private int _iHeight;

        #endregion

        #region property


        public Uri SiteUri
        {
            get
            {
                return _siteUri;
            }
            set
            {
                _siteUri = value;
            }
        }

        public Uri FileUri
        {
            get
            {
                return _fileUri;
            }
            set
            {
                _fileUri = value;
            }
        }

        public int Width
        {
            get
            {
                return _iWidth;
            }
            set
            {
                _iWidth = value;
            }
        }

        public int Height
        {
            get
            {
                return _iHeight;
            }
            set
            {
                _iHeight = value;
            }
        }

        #endregion

        #region constructor
        public ThumbnailBase(Uri FileURI)
        {
            this.FileUri = FileURI;
            this.Width = Helper.Img_Width;
            this.Height = Helper.Img_Height;
            
        }
        public ThumbnailBase()
        {}
        public ThumbnailBase(Uri FileURI, int Width, int Height)
        {
            this.FileUri = FileURI;
            this.Width = Width;
            this.Height = Height;
            
        }
        #endregion

        #region method
        public abstract Bitmap GenerateThumbnail();
        public abstract Bitmap GenerateThumbnail(int Page);
        public abstract void GenerateThumbnails();

        public abstract int GetCount();

        public virtual int GetCountThumbnailCache()
        {
            return Helper.GetCacheThumbnailCount(_fileUri);
        }

        public Stream GetFileStream()
        {
            using (SPSite siteCollection = new SPSite(_fileUri.AbsoluteUri))
            {
                Uri uriFile;
                Uri.TryCreate(new Uri(siteCollection.Url), FileUri, out uriFile);
                SPListItem li;
                using (SPWeb myWeb = siteCollection.OpenWeb())
                {
                    li = myWeb.GetListItem(uriFile.AbsoluteUri);
                }
                Stream str = li.File.OpenBinaryStream();
                return str;
            }
        
        }
        #endregion

    }
}
